
var util = require('../../../utils/util.js');
const app = getApp();
var url = app.globalData.url;
var imgURL = app.globalData.imgUrl; 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _num:1,
    ids: '',
    show: true,
    id: '',
    add: false,
    listData: [],
    listcart: [],
    items: 0,
    totalPrice: 0,
    type: [
      {
        typeid: 1,
        typename: '早餐',
        img:  "../../../image/breakfast.png",
      },
      {
        typeid: 2,
        typename: '午餐',
        img: "../../../image/lunch.png",
      },
      {
        typeid: 3,
        typename: '晚餐',
        img: "../../../image/dinner.png",
      },
      {
        typeid: 4,
        typename: '宵夜',
        img:  "../../../image/supper.png",
      }
    ],
  },
  goodsCarNumber:'',
  //选好了
  goBalance: function () {
    wx.navigateTo({
      url: '../cart/cart',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //跳转详情页
  detail: function (e) {
    var id = e.currentTarget.dataset.detailid;
    console.log(id);
    wx.navigateTo({
      url: '../listdetail/listdetail?id=' + id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //根据Id查询分类菜单
  selectMenu: function (e) {
    var that = this;
    var id = this.data.id;
    that.setData({
      _num: e.currentTarget.dataset.id
    })
    if (id == '今日菜品') {
      var typeid = e.currentTarget.dataset.id;
      console.log(typeid);
      if (typeid == 1) {
        wx.request({
          url: url + 'getTodaysBreakfast',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到当日早餐数据信息'){
              console.log(res.data.resultMsg)
            }else{
              that.setData({
                list: res.data.object.todaysBreakfast
              })
            }
          }
        })
      }
      if (typeid == 2) {
        wx.request({
          url: url + 'getTodaysLunch',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到当日午餐数据信息') {
              console.log(res.data.resultMsg)
            } else {
              that.setData({
                list: res.data.object.todaysLunch
              })
            }
          }
        })
      }
      if (typeid == 3) {
        wx.request({
          url: url + 'getTodaysSupper',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到当日晚餐数据信息') {
              console.log(res.data.resultMsg)
            } else {
              that.setData({
                list: res.data.object.todaysSupper
              })
            }
          }
        })
      }
      if (typeid == 4) {
        wx.request({
          url: url + '/getHotDishers',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到当日早餐数据信息') {
              console.log(res.data.resultMsg)
            } else {
              that.setData({
                list: res.data.object.hotDishers
              })
            }
          }
        })
      }
    } else {
      var typeid = e.currentTarget.dataset.id;
      console.log(typeid);
      var that = this;
      if (typeid == 1) {
        wx.request({
          url: url + 'getTomorrowsBreakfast',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到明日早餐数据信息') {
              console.log(res.data.resultMsg)
            } else {
              that.setData({
                list: res.data.object.tomorrowsBreakfast
              })
            }
          }
        })
      }
      if (typeid == 2) {
        wx.request({
          url: url + 'getTomorrowsLunch',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到明日午餐数据信息') {
              console.log(res.data.resultMsg)
            } else {
              that.setData({
                list: res.data.object.tomorrowsLunch
              })
            }
          }
        })
      }
      if (typeid == 3) {
        wx.request({
          url: url + 'getTomorrowsSupper',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到明日晚餐数据信息') {
              console.log(res.data.resultMsg)
            } else {
              that.setData({
                list: res.data.object.tomorrowsSupper
              })
            }
          }
        })
      }
      if (typeid == 4) {
        wx.request({
          url: url + 'getHotDishers',
          data: {
            pageIndex: 1,
            pageSize: 100
          },
          header: {
            "Content-Type": "application/json"
          },
          success: function (res) {
            if (res.data.resultMsg == '没有查到明日夜宵数据信息') {
              console.log(res.data.resultMsg)
            } else {
              that.setData({
                list: res.data.object.hotDishers
              })
            }
          }
        })
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var todayid = options.todayid;
    app.globalData.todayid = todayid;
    var tomorrowid = options.tomorrowid;
    app.globalData.tomorrowid = tomorrowid;
    wx.request({
      url: url + 'getTodaysBreakfast',
      data: {
        pageIndex: 1,
        pageSize: 100
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        if (res.data.resultMsg == '没有查到当日早餐数据信息') {
          console.log(res.data.resultMsg)
        } else {
          that.setData({
            list: res.data.object.todaysBreakfast
          })
        }
      }
    })
    that.query();   //查询购物车
    var id = this.data.id;
    if (id = todayid) {
      this.setData({
        id: '今日菜品'
      });
    }
    if (id = tomorrowid) {
      this.setData({
        id: '明日菜品'
      })
    }
    // wx.request({
    //   url: url + 'getHotDishers',
    //   data: {
    //     pageIndex: 1,
    //     pageSize: 100
    //   },
    //   header: {
    //     "Content-Type": "application/json"
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //     that.setData({
    //       list: res.data.object.hotDishers
    //     });
    //     
    //   }
    // });
  },
  onShow:function(){
    this.query();   //查询购物车
  },
  //添加菜品到购物车
  addToCart: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var price = e.currentTarget.dataset.price;
    var img = e.currentTarget.dataset.img;
    const userid = app.globalData.userid;
    console.log(id);
    var that = this;
    wx.request({
      url: url + 'insertGoodsInCartDing',
      data: {
        goodsId: id,
        goodsName: name,
        goodsPrice: price,
        goodsNumber: 1,
        goodsPic: img,
        cartType: 1,
        userId: userid,
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data.object);
        that.setData({
          add: true
        });
        that.query();
      }
    });
  },
  no: function () {
    var that = this;
    wx.showToast({
      title: '已经加过一次了！',
      icon: '',
      image: '',
      duration: 2000,
      mask: true,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //查询购物车列表
  query: function () {
    var that = this;
    const userid = app.globalData.userid;
    wx.request({
      url: url + 'getShopCart',
      data: {
        userId: userid,
        cartType: 1,
        pageIndex: 1,
        pageSize: 1000
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        that.goodsCarNumber = 0
        if (res.data.object.cartList != undefined){
          for (let i = 0; i < res.data.object.cartList.length; i++) {
            that.goodsCarNumber += res.data.object.cartList[i].goodsNumber
          }
          that.setData({
            goodsCarNumber: that.goodsCarNumber,
            listcart: res.data.object.cartList
          })
        } else {
          that.setData({
            goodsCarNumber: 0,
            listcart: ""
          })
        }
        that.getTotalPrice();
      }
    });
  },
  //跳转购物车列表
  cart: function () {
    wx.navigateTo({
      url: '../cart/cart',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  /**
   * 计算总价
   */
  getTotalPrice() {
    var that = this;
    var listcart = this.data.listcart;                 // 获取购物车列表
    var total = 0;
    for (var i = 0; i < listcart.length; i++) {        // 循环列表得到每个数据
      if (listcart[i]) {                     // 判断选中才会计算价格
        total += listcart[i].goodsNumber * listcart[i].goodsPrice;
        // 所有价格加起来
      }
    }
    that.setData({                               // 最后赋值到data中渲染到页面
      listcart: listcart,
      totalPrice: total
      // .toFixed(2)
    });
  }
})